using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Markup.Xaml.Templates;

namespace KinonekoSoftware.Foundation.UX.Controls
{
    public class BinaryControl : ToggleButton
    {
        static BinaryControl()
        {

            NegativeProperty         = AvaloniaProperty.Register<BinaryControl, object>(nameof(Negative));
            NegativeTemplateProperty = AvaloniaProperty.Register<BinaryControl, IDataTemplate>(nameof(NegativeTemplate));
            PositiveProperty         = AvaloniaProperty.Register<BinaryControl, object>(nameof(Positive));
            PositiveTemplateProperty = AvaloniaProperty.Register<BinaryControl, IDataTemplate>(nameof(PositiveTemplate));
            HeaderProperty           = AvaloniaProperty.Register<BinaryControl, string>(nameof(Header));
            ValueProperty            = AvaloniaProperty.Register<BinaryControl, string>(nameof(Value));
            ValueProperty.Changed.Subscribe(OnValueChanged);
        }


        public static readonly StyledProperty<object>        NegativeProperty;
        public static readonly StyledProperty<IDataTemplate> NegativeTemplateProperty;
        public static readonly StyledProperty<object>        PositiveProperty;
        public static readonly StyledProperty<IDataTemplate> PositiveTemplateProperty;
        public static readonly StyledProperty<string>        HeaderProperty;
        public static readonly StyledProperty<string>        ValueProperty;



        public string Value
        {
            get { return GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        
        private bool _valueChanged;
        private static void OnValueChanged(AvaloniaPropertyChangedEventArgs<string> e)
        {
            var ov = (BinaryControl)e.Sender;
            var v  = e.GetNewValue<string>();
            if (ov._valueChanged)
            {
                ov._valueChanged = false;
                return;
            }

            ov.IsChecked = v == ov.Positive?.ToString();

        }

        protected override void OnClick()
        {
            var check = IsChecked == true;

            Value = check ? Positive?.ToString() : Negative?.ToString();

            base.OnClick();
        }

        public string Header
        {
            get => GetValue(HeaderProperty);
            set => SetValue(HeaderProperty, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="PositiveTemplate"/> 属性。
        /// </summary>
        public IDataTemplate PositiveTemplate
        {
            get => (IDataTemplate)GetValue(PositiveTemplateProperty);
            set => SetValue(PositiveTemplateProperty, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="Positive"/> 属性。
        /// </summary>
        public object Positive
        {
            get => GetValue(PositiveProperty);
            set => SetValue(PositiveProperty, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="NegativeTemplate"/> 属性。
        /// </summary>
        public IDataTemplate NegativeTemplate
        {
            get => (IDataTemplate)GetValue(NegativeTemplateProperty);
            set => SetValue(NegativeTemplateProperty, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="Negative"/> 属性。
        /// </summary>
        public object Negative
        {
            get => GetValue(NegativeProperty);
            set => SetValue(NegativeProperty, value);
        }
    }
}